﻿using BJ.AuthDomain.IService;
using BJ.AuthEntity.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Syspetro.Core.AppDeal;
using Syspetro.Core.DynamicApi;
using Syspetro.Core.Extensions;
using Syspetro.Core.Localization;
using Syspetro.Core.Resources;
using Syspetro.Deploy.AuthorizationReq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BJ.AuthApp.Admin
{
  
    public class AdminAuthentication : BaseDynamicApi
    {
        private readonly IAdminUserAuthService _AuthService;

        public AdminAuthentication(IAdminUserAuthService authService)
        {
            _AuthService = authService;
        }
        /// <summary>
        /// 获取管理员ID
        /// </summary>
        /// <param name="loginUser"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SpActionResult<Guid>> LoginUser([FromBody] VM_AdminLogin loginUser)
        {
            var token = await _AuthService.LoginUser(loginUser);
            if (token != null)
            {
                return token;
            }
            else
            {
                return Fail<Guid>(SharedResource.PaswdError);
            }
        }
        /// <summary>
        /// google验证登录
        /// </summary>
        /// <param name="authLogin"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SpActionResult<VM_Token>> AuthLoginUser(VM_AdminAuthLogin authLogin)
        {
            var token = await _AuthService.AuthLoginUser(authLogin);
            if (token != null)
            {
                return token;
            }
            else
            {
                return Fail<VM_Token>(SharedResource.PaswdError);
            }
        }
        /// <summary>
        /// 获取用户详情
        /// </summary>
        /// <returns></returns>
        [Admin]
        public async Task<SpActionResult<VM_User>> GetUserInfo()
        {
            return await _AuthService.GetUserInfo();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="changePswd"></param>
        /// <returns></returns>
        [Admin]
        public async Task<SpActionResult<bool>> ChangePswd(VM_ChangePswd changePswd)
        {
            try
            {
                if (changePswd.Pswd_New == changePswd.Pswd_Old)
                    return Fail<bool>("原密码不能等于新密码");
                if (!changePswd.Pswd_New.IsPassword())
                    return Fail<bool>("密码格式不正确(密码至少包含 数字和英文，长度6-20)");
                return await _AuthService.ChangePswd(changePswd);
            }
            catch (Exception ex)
            {
                return Fail<bool>(ex.Message);
            }
        }
        /// <summary>
        /// 刷新Token
        /// </summary>
        /// <param name="id">TokenId</param>
        /// <returns></returns>
        [HttpPost("{id}")]
        public async Task<SpActionResult<string>> RefreshToken(Guid id)
        {
            var token = await _AuthService.RefreshToken(id);
            var msg = SharedResource.TokenExpire;//多语言
            if (token == null)
            {
                return Fail<string>(msg);
            }
            return Result(token);
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        /// <param name="id">TokenId</param>
        /// <returns></returns>
        [Admin]
        public async Task LogOut(Guid id)
        {
            await _AuthService.LogOut(id);
        }
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <returns></returns>
        [Admin]
        public async Task<SpActionResult<List<VM_Menu>>> GetAuthMenus()
        {
            return new SpActionResult<List<VM_Menu>>(await _AuthService.GetAuthMenus());
        }
    }
}
